/**
 * @使用数组指向指针,计算一个数组元素的和
 *
 * @author	 wangshikun <wangshikun2010@gmail.com>
 * @copyright	(c) 2012 wangshikun <wangshikun2010@gmail.com>
 * @license	http://www.opensource.org/licenses/mit-license.php The MIT License
 * @package	default
 * @subpackage	default
 */

#include <stdio.h>

// call the function
int array(int array[], const int n);

int main(void) {

	// Declare an array
	int values[10] = {3, 7, -9, 3, 6, -1, 7, 9, 1, -5};

	printf("The sum is %d\n", array(values, 10));

	return 0;
}

int array(int array[], const int n) {
	int sum = 0, *i;
	// 将arrayEnd指向数组的最后一个元素
	int * const arrayEnd = array + n;

	// 数组的第一个元素的值
	printf("array = %d\n", *array);
	// 数组的最后一个元素的索引
	printf("array + n = %d\n\n", *array + n);

	for (i = array; i < arrayEnd; i++) {
		sum += *i;
		printf("i = %d, sum = %d\n", *i, sum);
	}

	return sum;
}
